/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.launch.console;

import java.util.Locale;

import nemos.launch.progress.IProgressDisplay;

/**
 * SimpleConsoleProgress : The simple console progress.
 * 
 * <p>
 * This class display labels in the console with the current percent value. It
 * also display dots regularly in order to show launch activity.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class SimpleConsoleProgress implements IProgressDisplay {

    /** The current progress */
    private int mProgress;
    /** The last displayed label */
    private String mLastLabel;

    /**
     * Constructor
     */
    public SimpleConsoleProgress() {
        super();

        mProgress = 0;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.progress.IProgressDisplay#setProgressValue(int)
     */
    @Override
    public synchronized void setProgressValue(final int pProgressValue) {
        for (int i = (mProgress / 5); i < (pProgressValue / 5); i++) {
            System.out.print('.');
        }
        mProgress = pProgressValue;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.progress.IProgressDisplay#setLabel(java.lang.String)
     */
    @Override
    public synchronized void setLabel(final String pLabel) {
        if (pLabel != null && !pLabel.equals(mLastLabel)) {
            // Start a new line
            if (mProgress > 0) {
                System.out.println();
            }

            // Left padding with spaces
            final String lProgress = String.valueOf(Math.max(0, mProgress));
            for (int i = 0; i < 3 - lProgress.length(); i++) {
                System.out.print(' ');
            }
            // Progress
            System.out.print(lProgress);
            System.out.print("% "); //$NON-NLS-1$

            // Step label
            System.out.print(pLabel);

            // Ended : add a new line
            if (mProgress == 100) {
                System.out.println();
            }

            mLastLabel = pLabel;
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.progress.IProgressDisplay#setLaunchLocale(java.util.Locale)
     */
    @Override
    public void setLaunchLocale(@SuppressWarnings("unused") final Locale pLocale) {
        // Not used
    }
}
